import { combination } from "../../utils/util";

class SkuCode {
  code;
  spuId;
  totalSegments = []; // 一个sku所有可能的路径（可选择）

  constructor(code) {
    this.code = code;
    this._splitToSegments();
    console.log("totalSegments", this.totalSegments);
  }

  _splitToSegments() {
    /**
     * sku的code值 2$1-19#3-9#4-14 ({key_id: 1, key: "颜色", value_id: 19, value: "金色"})
     * 第一个值一定为spuId = 2
     * 第二个值 每一个sku的code 1-19#3-9#4-14 包含三个规格
     */
    const spuAndSpec = this.code.split("$");
    this.spuId = spuAndSpec[0];

    // 1-44#3-9#4-14
    const specCodeArray = spuAndSpec[1].split("#");
    const length = specCodeArray.length;

    for (let i = 1; i <= length; i++) {
      // 组合
      const segments = combination(specCodeArray, i);
      const newSegments = segments.map((segs) => {
        return segs.join("#");
      });
      this.totalSegments = this.totalSegments.concat(newSegments);
    }
  }
}

export { SkuCode };
